package com.powerunion.datacollection.web.struts;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.powerunion.datacollection.model.ApproveWorkItem;
import com.powerunion.datacollection.service.ActivityManager;
import com.powerunion.datacollection.service.ApproveWorkItemManager;
import com.powerunion.datacollection.service.SubmissionWorkItemManager;
import com.powerunion.datacollection.transaction.Transaction;
import com.powerunion.datacollection.transaction.TransactionHelper;
import com.powerunion.util.log.LogHelper;

public class ApproveWorkAction extends DispatchAction {

	private static final Logger logger = LogHelper.getInstance().getLogger(ApproveWorkAction.class);
	private  ApproveWorkItemManager awManager = null;
	
	public ApproveWorkAction() {
		super();
		this.awManager = new ApproveWorkItemManager();
	}
	
	public void finalize() throws Throwable {
		this.awManager = null;
		super.finalize();
	}
	
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String userCode = request.getRemoteUser();
		String result = this.awManager.findApproveWorkByUser(userCode);
		logger.debug("待办审批任务:"+result);
		response.setContentType("text/json;charset=utf-8");
		try{
			response.getWriter().print(result);
		}catch(Exception e) {
			logger.error("输出json失败",e);
			return null;
		}
		return null;
	}
	
	public ActionForward loadInfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		String id = request.getParameter("id");
		response.setContentType("text/json;charset=utf-8");
		String data = this.awManager.loadApproveInfo(id);
		logger.debug("loadInfo the ApproveInfo:" + data);
		response.getWriter().print(data);
		return null;
	}
	
	public ActionForward saveApproveInfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		response.setContentType("text/json;charset=utf-8");
		String formData = request.getParameter("formData");
		String actId = request.getParameter("actId");
		String approveWorkId = request.getParameter("id");
		String nextActivityId = request.getParameter("nextActivityId");
		String dynamicApproveUsers = request.getParameter("approveUsers");
		
		boolean isCommited = false;
		isCommited = this.awManager.iApproveWorkCommit(approveWorkId);
		if(isCommited){
			
			response.getWriter().write("{failure:true,data:'该任务已经结束！不能提交！'}");
			return null;
		}
		
		logger.debug("审批意见:"+formData);
		Transaction transaction = null;
	
		try{
			transaction = TransactionHelper.getTransaction();
			transaction.begin();
			boolean flag = this.awManager.saveApproveInfo(formData,actId,approveWorkId,nextActivityId,dynamicApproveUsers);
			
			if (flag == true) {
				response.getWriter().print("{success:true}");
			} else {
				response.getWriter().write("{failure:true}");
			}
			
			transaction.commit();
		} catch (Exception e) {
			logger.error("", e);
			try {
				transaction.rollback();
			} catch (Exception e1) {
				logger.error("", e1);
				throw e;
			} finally {
				transaction = null;
				response.getWriter().write("{failure:true}");
			}
		}
		return null;
	}
	
	public ActionForward submission(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String actId = request.getParameter("actId");
		String id = request.getParameter("id");
		String nextActivityId = request.getParameter("nextActivityId");
		String dynamicApproveUsers = request.getParameter("approveUsers");
		
		Transaction transaction = null;
		response.setContentType("text/json;charset=utf-8");
		try{
			transaction = TransactionHelper.getTransaction();
			transaction.begin();
			
			String result = "";
			result = this.awManager.submission(actId, id,nextActivityId,dynamicApproveUsers);
			
			if (StringUtils.isEmpty(result)) {
				response.getWriter().print("{success:true}");
			} else {
				response.getWriter().write("{failure:true,data:'"+result+"'}");
			}
			
			transaction.commit();
		} catch (Exception e) {
			logger.error("", e);
			try {
				transaction.rollback();
			} catch (Exception e1) {
				logger.error("", e1);
				throw e;
			} finally {
				transaction = null;
				response.getWriter().write("{failure:true}");
			}
		}
		return null;
	}
	
	public ActionForward getApproveInfoHistory(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String actId = request.getParameter("actId");
		int type = Integer.parseInt(request.getParameter("type")==null?"0":request.getParameter("type"));
		String result = this.awManager.approveInfoHistory(actId,type);
		logger.debug("approveInfoHistory:"+result);
		response.setContentType("text/json;charset=utf-8");
		try{
			response.getWriter().print(result);
		}catch(Exception e) {
			logger.error("输出json失败",e);
			return null;
		}
		return null;
	}
	
	public ActionForward getSubWorkUsers(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String approeWorkId = request.getParameter("approveId");
		ApproveWorkItem awi = this.awManager.getByKey(approeWorkId);
		
		String result = "";
		result = this.awManager.getSubWorkUsers(approeWorkId,awi.getPreActType());
		logger.debug("getSubWorkUsers:"+result);
		response.setContentType("text/json;charset=utf-8");
		try {
			response.getWriter().print(result);
		} catch (Exception e) {
			logger.error("输出json失败", e);
			return null;
		}
		return null;
	}
	
	public ActionForward completeWork(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String userCode = request.getRemoteUser();
		String result = this.awManager.findCompleteApproveWorkByUser(userCode);
		logger.debug("complete approve work:"+result);
		response.setContentType("text/json;charset=utf-8");
		try{
			response.getWriter().print(result);
		}catch(Exception e) {
			logger.error("输出json失败",e);
			return null;
		}
		return null;
	}

	
	public ActionForward allWork(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String userCode = request.getRemoteUser();
		String result = this.awManager.findAllApproveWorkByUser(userCode);
		logger.debug("all approve work:"+result);
		response.setContentType("text/json;charset=utf-8");
		try{
			response.getWriter().print(result);
		}catch(Exception e) {
			logger.error("输出json失败",e);
			return null;
		}
		return null;
	}
	
}
